package org.elephas.webapp.backend.component.management;

import java.io.Serializable;
import java.util.Iterator;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;
import org.elephas.model.Blog;
import org.elephas.persistence.query.QueryParam;
import org.elephas.persistence.query.Sort;
import org.elephas.service.BlogFinder;
import org.elephas.webapp.model.DetachableBlogModel;

import salve.depend.Dependency;

/**
 * @author Gerolf Seitz
 * 
 */
public class SortableBlogDataProvider extends SortableDataProvider {
	private static final long serialVersionUID = 1L;

	@Dependency
	private BlogFinder finder;

	public SortableBlogDataProvider() {
		setSort("name", true);
	}

	public Iterator<Blog> iterator(int first, int count) {
		QueryParam param = new QueryParam(first, count, newSort(getSort()));
		return finder.findBlogs(param).iterator();
	}

	public IModel model(Object object) {
		return new DetachableBlogModel((Blog) object);
	}

	public int size() {
		return finder.countBlogs();
	}

	public void detach() {

	}

	private Sort[] newSort(final SortParam sort) {
		return new Sort[] { new Sort() {

			public Serializable getData() {
				return sort.getProperty();
			}

			public boolean isAscending() {
				return sort.isAscending();
			}

		} };
	}
}
